﻿using System;
using System.Diagnostics;
using CommandLineParser.Exceptions;

namespace uTILLIty.ChartBuilder.Commandline
{
	class Program
	{
		static int Main(string[] args)
		{
			FileVersionInfo fileInfo = FileVersionInfo.GetVersionInfo(typeof(Program).Assembly.Location);
			Console.WriteLine(string.Format("{0} V. {1}", fileInfo.ProductName, fileInfo.ProductVersion));

			var parser = new CommandLineParser.CommandLineParser
			{
				ShowUsageOnEmptyCommandline = false,
				CheckMandatoryArguments = true,
				IgnoreCase = false,
				AcceptEqualSignSyntaxForValueArguments = true
			};
			var command = new ChartBuilderCommand();
			parser.ExtractArgumentAttributes(command);

			if (args.Length == 0)
			{
				parser.ShowUsage();
				return -1;
			}

			try
			{
				parser.ParseCommandLine(args);
				command.Execute();
				return 0;
			}
			catch (CommandLineException e)
			{
				Console.WriteLine(e.Message);
				parser.ShowUsage();
				return -3;
			}
			catch (Exception e)
			{
				Exception ex = e;
				while (ex.InnerException != null)
					ex = ex.InnerException;

				Console.Error.WriteLine("{0} ({1})", ex.Message, ex.GetType().Name);
				//throw;
				return -4;
			}
		}
	}
}
